package h0906;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
  * @description 无重复字符串的排列组合
  * @author 不知名帅哥
  * @date 2024/9/6 17:53
  * @version 1.0
*/

public class NoDuplicateString {
    public static void main(String[] args) {
        String[] res = permutation("pwe");
        Arrays.stream(res).forEach(System.out::println);
    }
    public static String[] permutation(String s) {
        List<String> res=new ArrayList<>();
        boolean[] used=new boolean[s.length()];
        backTrack(res,s,used,new StringBuilder());
        String[] array = res.toArray(new String[0]);
        return array;
    }

    private static void backTrack(List<String> res, String s, boolean[] used, StringBuilder temp) {
        if (temp.length()==s.length()){
            res.add(temp.toString());
            return;
        }
        for (int i = 0; i < s.length(); i++) {
            if (!used[i]){
                temp.append(s.charAt(i));
                used[i]=true;
                backTrack(res, s, used, temp);
                temp.setLength(temp.length()-1);
                used[i]=false;
            }
        }
    }
}
